#!/bin/bash
# animenu-fade-init
# Part of the tovid suite
# =======================
# A bash script that dynamically creates the fade algorithm 
# needed by animenu for menu fades
#
# Project homepage: http://www.tovid.org
#
#
# Copyright (C) 2005 tovid.org <http://www.tovid.org>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. Or see:
#
#           http://www.gnu.org/licenses/gpl.txt


# Mostly written by  Joe "Mac North" Friedrichsen:


# user-defined variables....
FRAME_RATE=${FRAME_RATE:-29.97}
OPACITY=${OPACITY:-100}
ANI_MENU_LENGTH=${ANI_MENU_LENGTH:-20}
DISSOLVE_LIMIT=$OPACITY

# get things in order
# ##########################
# Define variable limits....
FADE_FLOOR=0
FADE_CEILING=100
MIST_CEILING=33

# ##############
# New variables!
# fade times (in seconds)
BACKGROUND_FADE_LENGTH=0.7
TITLE_FADE_LENGTH=1.0
THUMBS_FADE_LENGTH=1.0
 
# fade in points (in seconds)
BACKGROUND_FADE_IN_START_TIME=0
BACKGROUND_FADE_IN_END_TIME=$(awk 'BEGIN { printf("'"$BACKGROUND_FADE_IN_START_TIME"'" + "'"$BACKGROUND_FADE_LENGTH"'")"\n" ; exit  }')
 
TITLE_FADE_IN_START_TIME=1.0
TITLE_FADE_IN_END_TIME=$(awk 'BEGIN { printf("'"$TITLE_FADE_IN_START_TIME"'" + "'"$TITLE_FADE_LENGTH"'") "\n" ; exit  }')
 
THUMBS_FADE_IN_START_TIME=${THUMBS_FADE_IN_START_TIME:-3.5}
THUMBS_FADE_IN_END_TIME=$(awk 'BEGIN { printf("'"$THUMBS_FADE_IN_START_TIME"'" + "'"$THUMBS_FADE_LENGTH"'") "\n" ; exit  }')
 
# fade out points (in seconds)
BACKGROUND_FADE_OUT_END_TIME=$ANI_MENU_LENGTH
BACKGROUND_FADE_OUT_START_TIME=$(awk 'BEGIN { printf("'"$BACKGROUND_FADE_OUT_END_TIME"'" - "'"$BACKGROUND_FADE_LENGTH"'") "\n" ; exit  }')
 
TITLE_FADE_OUT_END_TIME=$(awk 'BEGIN { printf("'"$ANI_MENU_LENGTH"'" - "'"$BACKGROUND_FADE_LENGTH"'")"\n" ; exit  }')

TITLE_FADE_OUT_START_TIME=$(awk 'BEGIN { printf("'"$TITLE_FADE_OUT_END_TIME"'" - "'"$TITLE_FADE_LENGTH"'")"\n" ; exit  }')
 
THUMBS_FADE_OUT_END_TIME=$(awk 'BEGIN { printf("'"$BACKGROUND_FADE_OUT_START_TIME"'" - "'"$THUMBS_FADE_LENGTH"'")"\n" ; exit  }')
THUMBS_FADE_OUT_START_TIME=$(awk 'BEGIN { printf("'"$THUMBS_FADE_OUT_END_TIME"'" - "'"$THUMBS_FADE_LENGTH"'")"\n" ; exit  }')
 
 
# ################################################
# Translate things for frame-by-frame construction
ANI_MENU_END_FRAME=$(awk -v fr=$FRAME_RATE -v al=$ANI_MENU_LENGTH 'BEGIN { printf("%d\n", (fr*al)); }')
 
BACKGROUND_FADE_IN_START_FRAME=$(awk -v fr=$FRAME_RATE -v bgfist=$BACKGROUND_FADE_IN_START_TIME 'BEGIN { printf("%d\n", (fr*bgfist)); }')

BACKGROUND_FADE_IN_END_FRAME=$(awk -v fr=$FRAME_RATE -v bgfiet=$BACKGROUND_FADE_IN_END_TIME 'BEGIN { printf("%d\n", (fr*bgfiet)); }')

TITLE_FADE_IN_START_FRAME=$(awk -v fr=$FRAME_RATE -v tfist=$TITLE_FADE_IN_START_TIME 'BEGIN { printf("%d\n", (fr*tfist)); }')

TITLE_FADE_IN_END_FRAME=$(awk -v fr=$FRAME_RATE -v tfiet=$TITLE_FADE_IN_END_TIME 'BEGIN { printf("%d\n", (fr*tfiet)); }')

THUMBS_FADE_IN_START_FRAME=$(awk -v fr=$FRAME_RATE -v thfist=$THUMBS_FADE_IN_START_TIME 'BEGIN { printf("%d\n", (fr*thfist)); }')

THUMBS_FADE_IN_END_FRAME=$(awk -v fr=$FRAME_RATE -v thfiet=$THUMBS_FADE_IN_END_TIME 'BEGIN { printf("%d\n", (fr*thfiet)); }')
 
BACKGROUND_FADE_OUT_START_FRAME=$(awk -v fr=$FRAME_RATE -v bgfost=$BACKGROUND_FADE_OUT_START_TIME 'BEGIN { printf("%d\n", (fr*bgfost)); }')

BACKGROUND_FADE_OUT_END_FRAME=$(awk -v fr=$FRAME_RATE -v bgfoet=$BACKGROUND_FADE_OUT_END_TIME 'BEGIN { printf("%d\n", (fr*bgfoet)); }')

TITLE_FADE_OUT_START_FRAME=$(awk -v fr=$FRAME_RATE -v tfost=$TITLE_FADE_OUT_START_TIME 'BEGIN { printf("%d\n", (fr*tfost)); }')

TITLE_FADE_OUT_END_FRAME=$(awk -v fr=$FRAME_RATE -v tfoet=$TITLE_FADE_OUT_END_TIME 'BEGIN { printf("%d\n", (fr*tfoet)); }')

THUMBS_FADE_OUT_START_FRAME=$(awk -v fr=$FRAME_RATE -v thfost=$THUMBS_FADE_OUT_START_TIME 'BEGIN { printf("%d\n", (fr*thfost)); }')

THUMBS_FADE_OUT_END_FRAME=$(awk -v fr=$FRAME_RATE -v thfoet=$THUMBS_FADE_OUT_END_TIME 'BEGIN { printf("%d\n", (fr*thfoet)); }')
 
# Set fade rates (rise over run or alpha over frame number)
BACKGROUND_FADE_RATE=$(echo "scale = 2; ($FADE_CEILING - $FADE_FLOOR) / ($FRAME_RATE * $BACKGROUND_FADE_LENGTH)" |bc)
TITLE_FADE_RATE=$(echo "scale = 2; ($FADE_CEILING - $FADE_FLOOR) / ($FRAME_RATE * $TITLE_FADE_LENGTH)" |bc)
MIST_FADE_RATE=$(echo "scale = 2; ($MIST_CEILING - $FADE_FLOOR) / ($FRAME_RATE * $TITLE_FADE_LENGTH)" |bc)
THUMBS_FADE_RATE=$(echo "scale = 2; ($DISSOLVE_LIMIT - $FADE_FLOOR) / ($FRAME_RATE * $THUMBS_FADE_LENGTH)" |bc)
 
get_bg_opacity ()
{
  if [[ 0 -le $frame && $frame -lt $BACKGROUND_FADE_IN_START_FRAME ]];  then D=$FADE_FLOOR; fi
  if [[ $BACKGROUND_FADE_IN_START_FRAME  -le $frame && $frame -lt $BACKGROUND_FADE_IN_END_FRAME ]]; then
      D=$(awk -v frame=$frame -v bfist=$BACKGROUND_FADE_IN_START_FRAME -v bfr=$BACKGROUND_FADE_RATE -v ff=$FADE_FLOOR 'BEGIN { printf("%d\n", (ff + ( (frame-bfist) * bfr) )); }')
  fi
  if [[ $BACKGROUND_FADE_IN_END_FRAME -le $frame && $frame -lt $BACKGROUND_FADE_OUT_START_FRAME ]]; then
      D=$FADE_CEILING
  fi
  if [[ $BACKGROUND_FADE_OUT_START_FRAME -le $frame && $frame -lt $BACKGROUND_FADE_OUT_END_FRAME ]]; then
      D=$(awk -v frame=$frame -v bfost=$BACKGROUND_FADE_OUT_START_FRAME -v bfr=$BACKGROUND_FADE_RATE -v fc=$FADE_CEILING 'BEGIN { printf("%d\n", (fc - ( (frame-bfost) * bfr) )); }')
  fi
  if [[ $BACKGROUND_FADE_OUT_END_FRAME -le $frame && $frame -le $ANI_MENU_END_FRAME ]]; then D=$FADE_FLOOR; fi
  echo $D
}
 
get_title_opacity ()
{
  if [[ 0 -le $frame && $frame -lt $TITLE_FADE_IN_START_FRAME ]]; then
      B=$FADE_FLOOR; C=$FADE_FLOOR
  fi
  if [[ $TITLE_FADE_IN_START_FRAME -le $frame && $frame -lt $TITLE_FADE_IN_END_FRAME ]]; then 
      B=$(awk -v frame=$frame -v tfisf=$TITLE_FADE_IN_START_FRAME -v mfr=$MIST_FADE_RATE  'BEGIN { printf("%d\n", ((frame-tfisf) * mfr)); }')
      C=$(awk -v frame=$frame -v tfisf=$TITLE_FADE_IN_START_FRAME -v tfr=$TITLE_FADE_RATE  'BEGIN { printf("%d\n", ((frame-tfisf) * tfr)); }')
  fi
  if [[ $TITLE_FADE_IN_END_FRAME -le $frame && $frame -lt $TITLE_FADE_OUT_START_FRAME ]]; then
      B=$MIST_CEILING; C=$FADE_CEILING
  fi
  if [[ $TITLE_FADE_OUT_START_FRAME -le $frame && $frame -lt $TITLE_FADE_OUT_END_FRAME ]]; then
      B=$(awk -v frame=$frame -v tfosf=$TITLE_FADE_OUT_START_FRAME -v mfr=$MIST_FADE_RATE -v mc=$MIST_CEILING 'BEGIN { printf("%d\n", (mc - ( (frame-tfosf) * mfr) )); }')
      C=$(awk -v frame=$frame -v tfosf=$TITLE_FADE_OUT_START_FRAME -v tfr=$TITLE_FADE_RATE -v fc=$FADE_CEILING  'BEGIN { printf("%d\n", (fc - ( (frame-tfosf) * tfr) )); }')
  fi
  if [[ $TITLE_FADE_OUT_END_FRAME -le $frame && $frame -le $ANI_MENU_END_FRAME ]]; then
      B=$FADE_FLOOR; C=$FADE_FLOOR
  fi
 
  echo $B:$C
}
 
get_thumb_opacity ()
{
  if [[ 0 -le $frame && $frame -lt $THUMBS_FADE_IN_START_FRAME ]];  then S=$FADE_FLOOR; fi
  if [[ $THUMBS_FADE_IN_START_FRAME -le $frame && $frame -lt $THUMBS_FADE_IN_END_FRAME ]]; then
      S=$(awk -v frame=$frame -v thfisf=$THUMBS_FADE_IN_START_FRAME -v thfr=$THUMBS_FADE_RATE -v ff=$FADE_FLOOR  'BEGIN { printf("%d\n", (ff + ( (frame-thfisf) * thfr) )); }')
  fi
  if [[ $THUMBS_FADE_IN_END_FRAME -le $frame && $frame -lt $THUMBS_FADE_OUT_START_FRAME ]]; then
      S=$DISSOLVE_LIMIT
  fi
  if [[ $THUMBS_FADE_OUT_START_FRAME -le $frame && $frame -lt $THUMBS_FADE_OUT_END_FRAME ]]; then
      S=$(awk -v frame=$frame -v thfosf=$THUMBS_FADE_OUT_START_FRAME -v thfr=$THUMBS_FADE_RATE -v dl=$DISSOLVE_LIMIT  'BEGIN { printf("%d\n", (dl - ( (frame-thfosf) * thfr) )); }')
  fi
  if [[ $THUMBS_FADE_OUT_END_FRAME -le $frame && $frame -le $ANI_MENU_END_FRAME ]]; then S=$FADE_FLOOR; fi
 
  echo $S
}

# Initialize a 'random' array of gamma values. There are 5 input variables:
#   $1 = the average gamma value
#   $2 = the magnitude of the main gamma variation (0.1 is a good start)
#   $3 = the time before the main variation repeats itself (in seconds)
#   $4 = the magnitude of the minor gamma variation (half of main is good)
#   $5 = the time before the minor variation repeats itself (in seconds)
#
#   OUTPUT = a newline separated list of numbers
#
# USAGE:
#           init_gamma_array average main_mag main_time minor_mag minor_time
# EXAMPLE:
#           GAMMA_VALUES="`init_gamma_array 1 0.1 5 0.05 0.1 | tr '\n' ' '`"
#           GAMMA_ARRAY=( "$GAMMA_VALUES" )

init_gamma_array ()
{
bc -l <<BC_EOF
pi=4*a(1);

start_frame=0

# Get values from standard in
# Format:
# echo "29.97\n 300\n 10\n .1\n 5\n .05\n .1\n" | bc -l
frame_rate=$FRAME_RATE;
end_frame=$ANI_MENU_END_FRAME;
average=$1;
amplitude=$2;
period=$3;
am_amplitude=$4;
am_period=$5;

scale=3;
for (i=start_frame; i <= end_frame; i++) {
  current_frame=i;
  x=current_frame / frame_rate;

  # AM modulation
  gamma=average + (amplitude + am_amplitude*s(x*2*pi/am_period)) * s(x*2*pi/period);

  print gamma, "\n";
}
quit
BC_EOF
}

# example usage
#while [[ $frame -le $ANI_MENU_END_FRAME ]]; do
#    BG=$(get_bg_opacity)
#    MIST_TEXT=$(get_title_opacity)
#    MIST=$(awk -F ':' '{print $1'}<<<$MIST_TEXT)
#    TEXT=$(awk -F ':' '{print $2'}<<<$MIST_TEXT)
#    MONTAGE=$(get_thumb_opacity)
#    time=$(echo "scale=2; $frame / $FRAME_RATE" | bc)s
#    echo "$frame ($time) | BG: $BG mist: $MIST text: $TEXT montage: $MONTAGE"
#    ((frame++))
#done
GAMMA_VALUES="`init_gamma_array 1 0.01 5 0.01 1 | tr '\n' ' '`"
GAMMA_ARRAY=( "$GAMMA_VALUES" )
CURVE_VARS=${CURVE_VARS:-"20 10 5 10 1"}
CURVE_VALUES="`init_gamma_array  $CURVE_VARS | tr '\n' ' '`"
CURVE_ARRAY=( $CURVE_VALUES )

